package com.winning.ds.role.service.impl;

import com.winning.ds.common.entity.SysQjcsEntity;
import com.winning.ds.common.entity.SysYyzcEntity;
import com.winning.ds.role.dao.ParamDao;
import com.winning.ds.role.service.ParamService;
import org.apache.commons.lang.StringUtils;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: plat
 * @description:
 * @author: nxs
 * @create: 2018-08-01 14:43
 **/
@Service
public class ParamServiceImpl implements ParamService {

    @Autowired
    private ParamDao paramDao;


    @Override
    public List<SysQjcsEntity> queryMenuByAppid(String yydm) {
        return paramDao.queryMenuByAppid(yydm);
    }

    @Override
    public List<SysQjcsEntity> queryParamList(String ssxt, String ssmk,String csmc) {


        List<SysQjcsEntity> list = paramDao.queryParamList(ssxt, ssmk,csmc);
        List<SysQjcsEntity> qjcsEntity = new ArrayList<>();
        if(list.size()>0) {
                  for (SysQjcsEntity sqe : list)
                  {
                      //下拉框 查询数据
                      if("1".equals(sqe.getSrfsdm()) || "3".equals(sqe.getSrfsdm()))
                      {
                         List<Map<String,Object>> optionList = getOptionList(sqe.getQzsql());
                         sqe.setOptionList(optionList);
                      }
                      //复选框处理
                      if("2".equals(sqe.getSrfsdm()))
                      {
                          if("1".equals(sqe.getCsz()) || "true".equals(sqe.getCsz()))
                          {
                              sqe.setChecked("1");
                          }
                      }
                      if(sqe.getCsz()==null || "null".equals(sqe.getCsz())){
                          sqe.setCsz("");
                      }

                      qjcsEntity.add(sqe);
                  }
              }
        return qjcsEntity;
    }

    @Override
    public List<SysYyzcEntity> queryApplication() {
        List<SysYyzcEntity> list =  paramDao.queryApplication();
        return list;
    }


    //根据qzsql 查询 参数
    @Override
    public List<Map<String,Object>> getOptionList(String qzsql)
    {
        List<Map<String,Object>> list = new ArrayList<>();
        try {
            if(StringUtils.isNotEmpty(qzsql))
            {
                list = paramDao.optionList(qzsql);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    //保存参数
    @Override
    @Transactional
    public void saveParams(Map<String, String> paramsMap) {

        for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            String[] paramArr=entry.getKey().split("#");

            if("2".equals(paramArr[1]))
            {
              SysQjcsEntity sqe =  paramDao.queryQjcsInfo(paramArr[0]);
                //如果整型或字符型，保存0.1
                if("1".equals(sqe.getCszlxdm()) || "0".equals(sqe.getCszlxdm()))
                {
                    if("on".equals(entry.getValue()))
                    {
                        paramDao.upadteParam(paramArr[0],"1");
                    }else if("0".equals(entry.getValue()))
                    {
                        paramDao.upadteParam(paramArr[0],"0");
                    }
                }else if("3".equals(sqe.getCszlxdm()))//如果布尔型
                {
                    if("on".equals(entry.getValue()))
                    {
                        paramDao.upadteParam(paramArr[0],"true");
                    }else if("0".equals(entry.getValue()))
                    {
                        paramDao.upadteParam(paramArr[0],"false");
                    }
                }

            }else
            {
                paramDao.upadteParam(paramArr[0],entry.getValue());
            }



        }
    }

    //保存参数
    @Override
    @Transactional
    public void updateDbPassWord(Map<String, String> paramsMap) {
        String update = paramsMap.get("update");
        String dbPassWord = paramsMap.get("dbPassWord");

        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword("winning");
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        SysQjcsEntity sysQjcsEntity = new SysQjcsEntity();
        sysQjcsEntity.setCsbm("dbPassWord");
        //解密 decrypt  //加密 encrypt
        if("encrypt".equals(update)){
            dbPassWord = encryptor.encrypt(dbPassWord);
            dbPassWord = "ENC("+dbPassWord+")";
            sysQjcsEntity.setCszsm("miWen");  //密文
        }else {
            dbPassWord = encryptor.decrypt(dbPassWord);
            sysQjcsEntity.setCszsm("mingWen"); //明文
        }
        sysQjcsEntity.setCsz(dbPassWord);
        paramDao.upadteQjcs(sysQjcsEntity);
    }
}
